package com.blog.controller;

import com.blog.basic.query.BasicQuery;
import com.blog.basic.query.PageList;
import com.blog.entity.AdminUser;
import com.blog.entity.Article;
import com.blog.entity.Category;
import com.blog.service.AdminUserService;
import com.blog.service.ArticleService;
import com.blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminUserController {
    @Autowired
    AdminUserService adminUserService;
    @Autowired
    ArticleService articleService;
    @Autowired
    CategoryService categoryService;

    /*/admin/loginout*/
    @RequestMapping("/loginout")
    public void getLoginOut(HttpSession httpSession, HttpServletResponse response){

        try {
            httpSession.invalidate();
            response.sendRedirect("http://localhost:8080/admin/login");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
    * @Description: 分页加查询
    * @Param:  basicQuery 查询参数
    * @return: 
    * @Author: Mr.Zhao
    * @Date: 2022/4/5
    */ 
    @RequestMapping("/list")
    public  String getListPage(BasicQuery basicQuery,Model model){

        //根据分页+查询
        PageList<Article> listPage = articleService.getListPage(basicQuery);


        //放到作用域
        model.addAttribute("listPage",listPage);

        //跳转的页面自己写
        return "admin/index";
    }


    /**
     * 登后台录页面
     * @return
     */
    @RequestMapping("login")
    public String admin (){

        return "admin/login";

    }

 /*   *//**
     * 后台管理文章集合
     * @param model
     * @return
     *//*
    @RequestMapping("/admin/list")
    public String admin(Model model) {
        model.addAttribute("articles", articleService.articleList());
        return "admin/index";
    }*/

//    /**
//     * 登录验证
//     * @param user
//     * @param model
//     * @return
//     */
//    @RequestMapping(value = "/dologin", method = RequestMethod.POST)
//    public String doLogin(HttpServletResponse response, AdminUser user, Model model){
//
//        if(adminUserService.login(user.getUsername(), user.getPassword())){
//            Cookie cookie = new Cookie(WebSecurityConfig.SESSION_KEY, user.toString());
//            response.addCookie(cookie);
//            model.addAttribute("user", user);
//            System.out.println(cookie.getName());
//
//            return "redirect:/admin";
//        }else {
//            model.addAttribute("error", "用户名或者密码错误");
//            System.out.println("failture");
//
//            return "admin/login";
//        }
//    }

    /**
     * 尝试登录
     * @param request
     * @param adminUser
     * @param model
     * @return
     */
    @RequestMapping(value = "dologin", method = RequestMethod.POST)
    public String doLogin(HttpServletRequest request, AdminUser adminUser, Model model, HttpSession httpSession,HttpServletResponse response) {
        System.out.println("Id:" + adminUser.getUserId() + "密码:" + adminUser.getPassword());
        if (adminUserService.login(adminUser.getUserId(), adminUser.getPassword())) {


            Cookie cookie = new Cookie("username", adminUser.getUserId());
            cookie.setMaxAge(7 * 24 * 60 * 60);
            response.addCookie(cookie);
            httpSession.setAttribute("admin",adminUser);

            model.addAttribute("user", adminUser);  // request.getSession().setAttribute("user", adminUser);

            return "redirect:/admin/list"; //保存登陆的用户信息
        } else {
            String  error = "账号或者密码错误";
            model.addAttribute("error", error);

            return "admin/login";
        }
    }
    @RequestMapping(method = RequestMethod.GET, value = "/dologin")
    public String doLogin(HttpServletRequest request) {
        if (request.getSession().getAttribute("user") == null) {
            return "admin/login";
        }
        return "redirect:/admin/list";
    }

    /**
     * 根据id删除博客
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        articleService.deleteArticleById(id);
        return "redirect:/admin/list";
    }
    /**
     * 博客修改
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/update/{id}")
    public String update(@PathVariable("id") Integer id, Model model) {

        Article article = articleService.getArticleById(id);//根据id查询修改的博客内容
        model.addAttribute("target", article);

        List<Category> categories = categoryService.list();//修改  博客类别
        model.addAttribute("categories", categories);

        model.addAttribute("article", new Article());
        return "admin/write";
    }
    /**
     * 创作
     * @param model
     * @return
     */
    @RequestMapping("write")
    public String write(Model model) {
        List<Category> categories = articleService.getCategories();
        categories.remove(0);
        model.addAttribute("categories", categories);
        return "admin/write";
    }

    /**
     * 判单博客发布与修改
     * @param article
     * @return
     */
    @RequestMapping(value = "write", method = RequestMethod.POST)
    public String write(Article article) {
        if (article.getId() == null) {
            System.out.println(article);
            articleService.writeBlog(article);
            System.out.println("博客发布成功");
        } else {
            articleService.updateBlog(article);
            System.out.println("博客修改成功");
        }
        return "redirect:/admin/list";
    }
}
